<template>
  <div class="me">
    <div class="profile">
      <div class="box">
        <div class="avatar">
          <van-image
            v-if="user?.name"
            :src="formatAvatar(user.name)"
            width="100%"
            height="100%"
            round
          />
        </div>
        <div class="name" v-if="user?.id">{{ user?.name }}</div>
        <van-button v-else type="primary" size="small" round @click="onLogin"
          >登录/注册</van-button
        >
      </div>
    </div>
    <div class="others">
      <van-cell-group inset>
        <van-cell title="我的发布" is-link @click="onPublish" v-if="user?.id" />
        <van-cell title="我的资料" is-link to="/profile" v-if="user?.id" />
        <van-cell title="关于我们" is-link to="/about" />
        <van-cell title="退出登录" is-link @click="onLogout" v-if="user?.id" />
      </van-cell-group>
    </div>
  </div>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { useUserStore } from '../../../store/user'
import { formatAvatar } from '../../../utils/format-avatar'

const userStore = useUserStore()
const { user } = storeToRefs(userStore)
const router = useRouter()

const onPublish = () => {
  router.push(`/publish/${user.value?.id}`)
}
const onLogin = () => {
  router.push('/login')
}
const onLogout = () => {
  router.push('/login')
  localStorage.clear()
  userStore.$reset()
}
</script>

<style lang="scss" scoped>
.me {
  .profile {
    height: 150px;
    background-color: #fff;
    margin: 15px;
    border-radius: 10px;
    display: flex;
    justify-content: center;
    .box {
      display: flex;
      justify-content: center;
      flex-direction: column;
      align-items: center;
      .avatar {
        height: 80px;
        width: 80px;
        text-align: center;
      }
      .name {
        text-align: center;
        font-size: 20px;
        font-weight: 600;
      }
    }
  }
}
</style>
